package com.cat.recruit.controller;

import com.cat.recruit.bean.request.AdminLoginRequest;
import com.cat.recruit.bean.request.UpdateAppointmentSlotRequest;
import com.cat.recruit.bean.request.UpdateUserStatusRequest;
import com.cat.recruit.common.result.Result;
import com.cat.recruit.config.JwtUserDetails;
import com.cat.recruit.service.AdminService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.cat.recruit.common.result.Result.success;

/**
 *
 */
@Tag(name = "管理员模块")
@RestController
@RequestMapping("/api/admin")
@Validated
public class AdminController {

    @Autowired
    AdminService adminService;

    /**
     * 管理员登录
     * @param adminLoginRequest 管理员信息
     * @return result
     */
    @Operation(summary = "管理员登录", description = "通过用户名和密码进行管理员登录")
    @PostMapping("/login")
    public Result login(
            @Parameter(description = "管理员登录请求对象", required = true)
            @RequestBody AdminLoginRequest adminLoginRequest
    ) {
        return adminService.login(
                adminLoginRequest.getUserName(),
                adminLoginRequest.getPassword()
        );
    }

    @Operation(summary = "管理员获取报名列表", description = "管理员获取报名列表")
    @GetMapping("/applyList")
    @PreAuthorize("hasAuthority(T(com.cat.recruit.common.constant.Role).ADMIN)")
    public Result getApplyList(
            @Parameter(description = "页码", example = "1", required = false)
            @RequestParam(defaultValue = "1")
            Integer pageNum,

            @Parameter(description = "每页数量", example = "100", required = false)
            @RequestParam(defaultValue = "100")
            @Max(value = 100, message = "每页数量不能超过100")
            Integer pageSize,

            @Parameter(description = "用户状态筛选条件,默认所有状态", required = false)
            @RequestParam
            String status,

            @Parameter(description = "考核方向筛选条件,默认所有方向", required = false)
            @RequestParam
            String direction

    ) {
        return adminService.getApplyList(pageNum, pageSize,status,direction);
    }

    /**
     * 管理员提交评分
     */
    @Operation(summary = "管理员提交评分", description = "管理员提交评分")
    @PostMapping("/apply/score")
    @PreAuthorize("hasAuthority(T(com.cat.recruit.common.constant.Role).ADMIN)")
    public Result submitScore(
            @Parameter(description = "考核ID", required = true)
            Integer accessId,
            @Parameter(description = "评分", required = true)
            Integer score,
            @Parameter(description = "评语", required = true)
            String comment,
            @AuthenticationPrincipal JwtUserDetails jwtUserDetails
    ){
        return adminService.submitScore(accessId, score, comment, jwtUserDetails);
    }

    /**
     * 新增时间管理
     */
    @Operation(summary = "新增预约时间", description = "新增预约时间")
    @PostMapping("/addAppointmentTime")
    @PreAuthorize("hasAuthority(T(com.cat.recruit.common.constant.Role).ADMIN)")
    public Result addAppointmentTime(
            @Parameter(description = "时间所在的考核类型(如一面，一轮考核)", required = true)
            String accessType,
            @Parameter(description = "方向,如前端，后端",required = true)
            String direction,
            @Parameter(description = "预约人数上限",required = true)
            Integer interviewNumber,
            @Parameter(description = "预约日期:yyyy-MM-dd", required = true)
            @Pattern(regexp = "^\\d{4}-\\d{2}-\\d{2}$", message = "日期格式应为 yyyy-MM-dd")
            String appointmentDate,
            @Parameter(description = "开始时间:HH:mm:ss", required = true)
            @Pattern(regexp = "^([01]\\d|2[0-3]):([0-5]\\d):([0-5]\\d)$", message = "时间格式应为 HH:mm:ss")
            String startTime,
            @Parameter(description = "结束时间:HH:mm:ss", required = true)
            @Pattern(regexp = "^([01]\\d|2[0-3]):([0-5]\\d):([0-5]\\d)$", message = "时间格式应为 HH:mm:ss")
            String endTime
    ){
        return adminService.addAppointmentTime(accessType,direction,interviewNumber,appointmentDate, startTime, endTime);
    }

    /**
     * 管理员获取预约时间列表
     */
    @Operation(summary = "管理员获取预约时间列表", description = "管理员获取预约时间列表")
    @GetMapping("/appointmentTimeList")
    public Result getAppointmentTimeList(
    ){
        return adminService.getAppointmentTimeList();
    }

    /**
     * 获取用户信息
     */
    @Operation(summary = "获取用户信息", description = "获取用户信息")
    @GetMapping("/getUserInfo")
    public Result getUserInfo(
            @Parameter(description = "用户ID", required = true)
            @RequestParam(value = "userId")
            Integer userId
    ){
        return adminService.getUserInfo(userId);
    }

    /**
     * 设置招新状态
     */
    @Operation(summary = "设置招新状态", description = "设置招新状态")
    @PostMapping("/setRecruitStatus")
    public Result setRecruitStatus(
            @Parameter(description = "招新状态", required = true)
            String status
    ){
        return adminService.setRecruitStatus(status);
    }

    /**
     * 根据预约时间获取用户
     */
    @Operation(summary = "根据预约时间获取用户", description = "根据预约时间获取用户")
    @GetMapping("/getUsersByAppointmentTime")
    public Result getUsersByAppointmentTime(
            @Parameter(description = "预约时间id", required = true)
            @RequestParam(value = "appointmentSlotId")
            Integer appointmentSlotId
    ){
        return adminService.getUsersByAppointmentTime(appointmentSlotId);
    }

    /**
     * 获取用户的评分
     */
    @Operation(summary = "获取用户的评分", description = "获取用户的评分")
    @GetMapping("/getScore")
    public Result getScore(
            @Parameter(description = "用户ID", required = true)
            @RequestParam(value = "userId")
            Integer userId
    ){
        return adminService.getScore(userId);
    }

    /**
     * 删除预约时间段
     */
    @Operation(summary = "删除预约时间段", description = "删除预约时间段")
    @PostMapping("/deleteAppointmentSlot")
    public Result deleteAppointmentSlot(
            @Parameter(description = "预约时间id", required = true)
            @RequestParam(value = "appointmentSlotId")
            Integer appointmentSlotId
    ){
        return adminService.deleteAppointmentSlot(appointmentSlotId);
    }

    /**
     * 更新预约时间段
     */
    @Operation(summary = "更新预约时间段", description = "更新预约时间段")
    @PostMapping("/updateAppointmentSlot")
    public Result updateAppointmentSlot(
            @RequestBody
            UpdateAppointmentSlotRequest updateAppointmentSlotRequest
    ){
        return adminService.updateAppointmentSlot(
                updateAppointmentSlotRequest.getId(),
                updateAppointmentSlotRequest.getAccessType(),
                updateAppointmentSlotRequest.getDirection(),
                updateAppointmentSlotRequest.getAppointmentDate(),
                updateAppointmentSlotRequest.getStartTime(),
                updateAppointmentSlotRequest.getEndTime(),
                updateAppointmentSlotRequest.getCapacity()
        );
    }

    /**
     * 更新用户状态
     */
    @Operation(summary = "更新用户状态", description = "更新用户状态")
    @PostMapping("/updateUserStatus")
    public Result updateUserStatus(
            @RequestBody
            UpdateUserStatusRequest updateUserStatusRequest
    ){
        return adminService.updateUserStatus(
                updateUserStatusRequest.getUserId(),
                updateUserStatusRequest.getStatus()
        );
    }

    /**
     * 根据预约信息生成考核信息
     */
    @Operation(summary = "根据预约信息生成考核信息", description = "根据预约信息生成考核信息")
    @PostMapping("/generateAccess")
    public Result generateAccess(
            @Parameter(description = "考核类型", required = true)
            @RequestParam(value = "accessType")
            @NotEmpty(message = "考核类型不能为空")
            String accessType
    ){
        return adminService.generateAccess(accessType);
    }
}
